package com.ycwl.freetv.program;

import android.content.Context;
import android.os.Environment;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ycwl.library.ParseUIFromJson;

import java.io.File;
import java.net.ContentHandler;
import java.util.ArrayList;

public class Program implements ProgramInterface {
    public static class Builder {
        Context context;
        ParseUIFromJson.MProgram mProgram;
        public Builder(Context context, ParseUIFromJson.MProgram mProgram) {
            this.context = context;
            this.mProgram = mProgram;
        }

        public Program build() {
            return new Program(this);
        }
    }

    final Context applicationContext;
    final Context context;
    final RelativeLayout relativeLayout;
    private ArrayList<ProgramInterface> programItemList;


    public Program(Builder builder) {
        this.programItemList = new ArrayList<>();
        this.applicationContext = builder.context.getApplicationContext();
        this.context = builder.context;
        RelativeLayout relativeLayout1 = new RelativeLayout(this.applicationContext);
        ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(960, 540);
        marginLayoutParams.leftMargin = builder.mProgram.view.x.intValue();
        marginLayoutParams.topMargin = builder.mProgram.view.y.intValue();
        marginLayoutParams.width = builder.mProgram.view.width.intValue();
        marginLayoutParams.height = builder.mProgram.view.height.intValue();
        relativeLayout1.setLayoutParams(marginLayoutParams);

        for (ParseUIFromJson.BaseNode baseNode: builder.mProgram.view.children) {
            if (baseNode.type == ParseUIFromJson.NODE_TYPE.IMAGE) {
                Image image = new Image(context,baseNode);
                relativeLayout1.addView(image.getView());
                this.programItemList.add(image);
            } else if (baseNode.type == ParseUIFromJson.NODE_TYPE.TEXT) {
                Text text = new Text.Builder(context,baseNode).build();
                relativeLayout1.addView(text.getView());
                this.programItemList.add(text);
            } else if (baseNode.type == ParseUIFromJson.NODE_TYPE.VIDEO) {
                Video video = new Video(this.context, baseNode);
                relativeLayout1.addView(video.getView());
                this.programItemList.add(video);
            } else if (baseNode.type == ParseUIFromJson.NODE_TYPE.SLIDE) {

                Slide slide = new Slide(this.context, baseNode);
                relativeLayout1.addView(slide.getView());
                this.programItemList.add(slide);
            }
        }
        this.relativeLayout = relativeLayout1;

    }

    @Override
    public void play() {
        for (ProgramInterface item : this.programItemList) {
            item.play();
        }
    }

    @Override
    public void stop() {
        for (ProgramInterface item : this.programItemList) {
            item.stop();
        }
    }

    @Override
    public void setTotalTime(int totalTime) {

    }

    @Override
    public void pause() {
        for (ProgramInterface item : this.programItemList) {
            item.pause();
        }
    }

    @Override
    public View getView() {
        return this.relativeLayout;
    }
}
